<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/6/12
 * Time: 上午9:19
 */

namespace Modules\Admin\Auth\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AdminRole;

class AdminRoleController extends Controller
{
    public function index()
    {
        return view('admin.auth::role', \Request::all());
    }

    public function resource()
    {
        return success(config('resource.admin'));
    }

    public function all()
    {
        $data['items'] = AdminRole::get();
        $data['items']->each(function ($item) {
            $item->resource_names = $item->getResourceNames();
        });
        return success($data);
    }

    public function store(Request $request)
    {
        $name           = $request->input('name');
        $resource       = $request->input('resource');
        $role           = new AdminRole();
        $role->name     = $name;
        $role->resource = $resource;
        $role->save();
        $this->recordAction('添加了新角色' . $name);
        return success($role);
    }

    public function update(Request $request, $id)
    {
        $role = AdminRole::find($id);
        if ($request->has('name')) {
            $role->name = $request->input('name');
        }
        if ($request->has('resource')) {
            $role->resource = $request->input('resource');
        }
        $role->save();

        $this->recordAction('更新了角色权限',$role->toArray());

        return success($role);
    }

    public function destroy($id)
    {
        $role = AdminRole::find($id);
        $this->recordAction('删除了角色',$role->toArray());
        $role->delete();
    }
}